﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_UpdateStaffInfo]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_UpdateStaffInfo];
GO
CREATE PROCEDURE [dbo].[sproc_UpdateStaffInfo]
    @StaffID int,
    @RealName nvarchar(50),
    @Sex bit,
    @Birthday DateTime,
    @Password nvarchar(255),
    @Email nvarchar(500),
    @Phone nvarchar(50),
    @Mobile nvarchar(50),
    @PositionID int,
    @Caste int =0,
    @PassCheck bit = 0,
    @ModifyPassword bit = 1

AS

IF @PassCheck = 1 
BEGIN
    IF EXISTS(SELECT 1 FROM uds_staff WHERE staff_id = @staffid and [password] = @password)
        RETURN -1
END 

IF @ModifyPassword = 1
BEGIN
    UPDATE UDS_Staff 
        SET RealName=@RealName,
            Sex=@Sex,
            Birthday = @Birthday,
            [Password] = @password,
                Email = @Email,
            Phone = @Phone,
            Mobile=@Mobile,
            Caste = @Caste
        WHERE Staff_ID=@StaffID
    UPDATE UDS_staff_in_position
        SET position_id = @positionID
        WHERE STAFF_ID = @staffid
END
ELSE
BEGIN
    UPDATE UDS_Staff 
        SET RealName=@RealName,
            Sex=@Sex,
            Birthday = @Birthday,
                Email = @Email,
            Phone = @Phone,
            Mobile=@Mobile,
            Caste = @Caste
        WHERE Staff_ID=@StaffID
    UPDATE UDS_staff_in_position
        SET position_id = @positionID
        WHERE STAFF_ID = @staffid
END

RETURN  0